package com.itheima.servlet;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Keyword;
import com.itheima.service.impl.KeywrodServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/keyWord")
public class KeyWordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求参数
        String wd = request.getParameter("wd");
        //2. 创建service实现类对象
        KeywrodServiceImpl keywrodService = new KeywrodServiceImpl();
        //3. 调用service实现类对象的find()方法进行业务逻辑处理，并返回List<KeyWord>
        List<Keyword> keywords = keywrodService.find(wd);
        //处理响应乱码
        response.setContentType("text/html;charset=utf-8");
        //4.将List集合转换为json字符串
//        String jsonString = JSON.toJSONString(keywords);
        //5.响应回给浏览器
        response.getWriter().print(JSON.toJSONString(keywords));
    }
}